package cn.iocoder.yudao.module.game.service.bankCard;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.bankCard.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.bankCard.BankCardDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.bankCard.BankCardMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 银行卡 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BankCardServiceImpl implements BankCardService {

    @Resource
    private BankCardMapper bankCardMapper;

    @Override
    public Long createBankCard(BankCardSaveReqVO createReqVO) {
        // 插入
        BankCardDO bankCard = BeanUtils.toBean(createReqVO, BankCardDO.class);
        bankCardMapper.insert(bankCard);
        // 返回
        return bankCard.getId();
    }

    @Override
    public void updateBankCard(BankCardSaveReqVO updateReqVO) {
        // 校验存在
        validateBankCardExists(updateReqVO.getId());
        // 更新
        BankCardDO updateObj = BeanUtils.toBean(updateReqVO, BankCardDO.class);
        bankCardMapper.updateById(updateObj);
    }

    @Override
    public void deleteBankCard(Long id) {
        // 校验存在
        validateBankCardExists(id);
        // 删除
        bankCardMapper.deleteById(id);
    }

    private void validateBankCardExists(Long id) {
        if (bankCardMapper.selectById(id) == null) {
            throw exception(BANK_CARD_NOT_EXISTS);
        }
    }

    @Override
    public BankCardDO getBankCard(Long id) {
        return bankCardMapper.selectById(id);
    }

    @Override
    public PageResult<BankCardDO> getBankCardPage(BankCardPageReqVO pageReqVO) {
        return bankCardMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BankCardDO> getBankCardList(BankCardPageReqVO pageReqVO) {
        return bankCardMapper.selectList(new LambdaQueryWrapper<BankCardDO>()
                .eq(BankCardDO::getMemberId, pageReqVO.getMemberId()));
    }

}